﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string s = label1.Text;
            string creating = "Creating createLog file";
            string t = textBox1.Text;

            label1.Text= creating + t;

                createLog(1);

            
            //Form1.ActiveForm.Close();
            //WindowsFormsApplication1.xFiles j = new WindowsFormsApplication1.xFiles();
            //j.recreateLogfile();

                label1.Text =  s;
                MessageBox.Show("created");
                Form1.ActiveForm.Close();

        }
        public void createLog(int k)
        {
            switch (k)
            {
                case 1:
                    appStrartLog();
                    //write log
                    
                    break;
                case 2:
                    startWithWindows();
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                    {

                    }
                    default:            
            ErrorSomething();
         
            break;      
       }
       

                    string name;
                    string txt;
                    txt = ".txt";
                    string logname1;
                    name = textBox1.Text;
                    logname1 = name + txt;
                    // MessageBox.Show("logfilename {0} ", logname);


                   // MessageBox.Show(String.Format("The logfile name is  {0} ", logname1), "Results", MessageBoxButtons.OK);
                    // create the filename object - the while loop allows
                    // us to keep trying with different filenames until
                    // we succeed
                    StreamWriter sw = null;
                    string logname = "";


                    logname = logname1;
                    // MessageBox.Show("logfilename {0} ", logname);



                    //while (true)
                    //{
                    //try
                    //{




                    //logname 
                    if (logname.Length == 0)
                    {
                        // no filename - this jumps beyond the while
                        // loop to safety
                        //break;
                    }
                    // open file for writing; throw an exception if the
                    // file already exists:
                    // FileMode.CreateNew to create a file if it
                    // doesn't already exist or throw
                    // an exception if file exists
                    // FileMode.Append to create a new file or append
                    // to an existing file
                    // FileMode.Create to create a new file or
                    // truncate an existing file
                    // FileAccess possibilities are:
                    // FileAccess.Read,
                    // FileAccess.Write,
                    // FileAccess.ReadWrite
                    // FileStream fs = File.Open(logname,FileMode.CreateNew,FileAccess.Write);
                    //try
                    //{

                    //    FileStream fs = File.Open(logname, FileMode.Create, FileAccess.Write);
                    //}
                    //catch (IOException fe)
                    //{
                    //             MessageBox.Show("error") ;    // generate a file stream with UTF8 characters;

                    //}

                    FileStream fs = File.Open(logname, FileMode.Append, FileAccess.Write);
                    sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
                    // read one string at a time, outputting each to the
                    // FileStream open for writing
                    //new log entry
                    string logentrynew;
                    logentrynew = appStrartLog();



                    string sInput = logentrynew;
                    //if (sInput.Length == 0)
                    //{
                    //    break;
                    //}
                    // write the line just read to output file
                    sw.WriteLine(sInput);


                    // close the file we just created
                    sw.Close();
                    sw = null;
            //refresh the rich text viewer
                    WindowsFormsApplication1.Form1 formfirst = new WindowsFormsApplication1.Form1();
                    formfirst.refreshrichtext();
            }

public string ErrorSomething()
{
 	string logentrynew;
            string date;
            string logentry;
            DateTime n = DateTime.Parse("6/22/2009 07:00:00 AM");

            logentry = " Terminator X Created and error ";
            date = n.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
            logentrynew= date + logentry + date;
            return (logentrynew);       
}
        
public string startWithWindows()
{
       string logentrynew;
            string date;
            string logentry;
           // DateTime n = DateTime.UtcNow; works badly
            DateTime n = DateTime.Parse("6/22/2009 07:00:00 AM");

             

            logentry = " Terminator X Set to start with windows ";
            date = n.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
            logentrynew= date + logentry + date;
            return (logentrynew);            
} 
    public string appStrartLog()
        {
            string logentrynew;
            string date;
            string logentry;
            DateTime n = DateTime.Parse("6/22/2009 07:00:00 AM");

            logentry = " Terminator X Started at ";
            date = n.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
            logentrynew= date + logentry + date;
            return (logentrynew);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string dr = Directory.GetCurrentDirectory();
            System.Diagnostics.Process.Start("Log1.txt");
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
           
        }
        private void Form1_Resize(object sender, EventArgs e)
        {

            //if form is minimized then it will not be displayed in taskbar.

            if (FormWindowState.Minimized == this.WindowState)

                Hide();

        }



        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {

            //this will maximize the application minimized to System tray.

            Show();

            WindowState = FormWindowState.Normal;

        }


            //    catch (IOException fe)
            //    {
            //        // whoops -- an error occurred somewhere during
            //        // the processing of the file - tell the user
            //        // what the full name of the file is:
            //        // take the path name onto the filename
            //        string sDir = Directory.GetCurrentDirectory();
            //        string s = Path.Combine(sDir, logname);
            //        Console.WriteLine("Error on file {0}", s);
            //        // now output the error message in the exception
            //        Console.WriteLine(fe.Message);
            //    }
            //}
            // wait for user to acknowledge the results
            //Console.WriteLine("Press Enter to terminate...");
            //Console.Read();
        }
    }



       
    

